#!/bin/bash
cd ${0%/*} || exit 1    # Run from this directory

# Source tutorial run functions
. $WM_PROJECT_DIR/bin/tools/RunFunctions

runApplication blockMesh
runApplication foamPostProcess -func setVortexVelocity
runApplication foamRun

# Mesh bounds and centre
x0="$(foamDictionary system/blockMeshDict -entry x0 -value)"
x1="$(foamDictionary system/blockMeshDict -entry x1 -value)"
xC="($x0+$x1)/2"
y0="$(foamDictionary system/blockMeshDict -entry y0 -value)"
y1="$(foamDictionary system/blockMeshDict -entry y1 -value)"
yC="($y0+$y1)/2"

# Mesh number of divisions
n="$(foamDictionary system/blockMeshDict -entry n -value)"

# Injection position and radius
p=($(foamDictionary constant/Lagrangian/linear/LagrangianModels -entry pointInjection/position -value))
p=(${p[@]:1:3})
r="((${p[0]}-$xC)**2+(${p[1]}-$yC)**2)**0.5"

# Plot files
fileHead="postProcessing/"
fileTail="PositionPlot/0/LagrangianFieldValue.dat"

gnuplot << EOF

set terminal postscript eps color enhanced size 3,4
set output "position.eps"

set multiplot

set lmargin at screen 0.1
set rmargin at screen 0.9
set tmargin at screen 0.95
set bmargin at screen 0.35
set xtics $x0,($x1-$x0)/$n,$x1 scale 0 format ''
set ytics $y0,($y1-$y0)/$n,$y1 scale 0 format ''
set grid xtics ytics
set parametric
set samples 40

plot [0:2*pi][$x0:$x1][$y0:$y1] \
    "${fileHead}linear${fileTail}" us 2:3 w l t 'Linear', \
    "${fileHead}parabolic${fileTail}" us 2:3 w l t 'Parabolic', \
    $xC+$r*cos(t),$yC+$r*sin(t) w p pt 7 t 'Analytic'

unset margin
set size 0.85,0.3
set origin 0.1,0.02
set xtics autofreq scale 1 format
set ytics autofreq scale 1 format
unset grid
set xlabel "Time [s]"
set ylabel "Error [mm]"
set log y

plot \
    "${fileHead}linear${fileTail}" \
    us 1:(1e3*(((\$2-$xC)**2+(\$3-$yC)**2)**0.5-$r)) w l t 'Linear', \
    "${fileHead}parabolic${fileTail}" \
    us 1:(1e3*(((\$2-$xC)**2+(\$3-$yC)**2)**0.5-$r)) w l t 'Parabolic'

unset multiplot

EOF

#------------------------------------------------------------------------------
